package tju.mic.medicine.ws.resource;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import tju.mic.medicine.ws.models.Disease;
import tju.mic.medicine.ws.models.DiseaseList;
import tju.mic.medicine.ws.service.DiseaseService;
import tju.mic.utils.ApplicationContextProvider;


@Path("disease")
public class DiseaseResource {
    
    private DiseaseService diseaseService = ApplicationContextProvider.getApplicationContext().getBean(DiseaseService.class);
    
    @GET
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public Disease getById(@QueryParam("sid")String sid,@QueryParam("did")Long diseaseId) throws Exception{
        return diseaseService.getDiseaseById(diseaseId);
    }
    
    @POST
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public Disease save(@QueryParam("sid")String sid,Disease newDisease) throws Exception{
        return diseaseService.saveDisease(newDisease);
    }
    
    @PUT
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public Disease update(@QueryParam("sid")String sid,Disease newDisease) throws Exception{
        return diseaseService.saveDisease(newDisease);
    }
    
    @DELETE
    public void delete(@QueryParam("sid") String sid, @QueryParam("did")Long diseaseId) throws Exception{
        try {
            diseaseService.deleteDisease(diseaseId);
        } catch (Exception ex) {
            Logger.getLogger(DiseaseResource.class.getName()).log(Level.SEVERE, null, ex);
            throw ex;
        }
    }
    
    @GET
    @Path("list_by_depart")
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public DiseaseList getListByDepartId(@QueryParam("sid")String sid,@QueryParam("did")Long departId) throws Exception{
        return diseaseService.getListByDepart(departId);
    }
}
